Ext.namespace("Hroze");
/**
 * 扩展的简单窗口
 * @class Hroze.Window 扩展窗口
 * @extends Ext.util.Observable
 */
Hroze.Window = Ext.extend(Ext.util.Observable, {
	width : 360,
	height : 290,
	innerHTML : '',
	title : '',
	el : '',
	mask : true,
	/**
	 * 
	 * @type {array} els 元素名称
	 */
	names : ['mask', 'shim', 'el', 'title', 'text', 'close', 'content'],
	els : {},
	group : "ux-window-group",
	model : '<div id="{id}" name="win" class="ux-window">'
			+ '<div name="mask" class="ux-window-mask"></div>'
			+ '<iframe name="shim" class="ux-window-shim" frameborder="0"></iframe>'
			+ '<div name="el" class="ux-window-el">'
			+ '<div name="title" class="ux-window-title">'
			+ '<div name="text" class="ux-window-title-text">{title}</div>'
			+ '<div name="close" class="ux-window-close">关闭</div>' + '</div>'
			+ '<div name="content" class="ux-window-content"></div>' + '</div>'
			+ '</div>',
	constructor : function(config) {
		Ext.apply(this, config);
		Hroze.Window.superclass.constructor.call(this);
		this.addEvents("show", "close", "hide");
		this.init();
	},
	/**
	 * 生成容器
	 */
	initGroup : function() {
		var g = Ext.get(this.group);
		if (!g) {
			this.group = Ext.getBody().insertFirst({
						id : this.group,
						tag : 'DIV',
						cls : 'ux-window-group'
					});
			this.group.setVisibilityMode(Ext.Element.DISPLAY);
			//this.group.hide();
		} else {
			this.group = g;
		}
	},
	init : function() {
		this.initGroup();
		this.tpl = new Ext.Template(this.model);
		this.tpl.compile();
		if (Ext.isEmpty(this.id)) {
			this.id = Ext.id();
		}
		this.win = this.tpl.append(this.group, {
					id : this.id,
					title : this.title
				}, true);
		Ext.each(this.names, function(item, index, arr) {
					this.els[item] = this.win.child('*[name=' + item + ']');
					this.els[item].setVisibilityMode(Ext.Element.DISPLAY);
					this.els[item].hide();
				}, this);
		this.el = this.els.el;
		this.els.content.update(this.innerHTML);
		if (Ext.isNumber(this.height)) {
			this.el.setHeight(this.height);
		}
		if (Ext.isNumber(this.width)) {
			this.el.setWidth(this.width);
			this.els.text.setWidth(this.width - 50);
		}
		this.addEvent();
		if ("off" == this.closeState) {
			this.disableClose();
		} else {
			this.closeState = "on";
		}
	},
	addEvent : function() {
		this.els.close.hover(this.hover, this.hover, this);
		this.els.close.on("click", this.close, this);
	},
	hover : function(e, t) {
		e.preventDefault();
		Ext.fly(t).toggleClass("ux-window-close-hover");
	},
	getHeight : function() {
		var a = document.documentElement.scrollHeight;
		var c = document.body.scrollHeight;
		var b = Ext.lib.Dom.getViewHeight();
		if (c > b) {
			b = c;
		}
		return b > a ? b : a;
	},
	show : function() {
		// 显示全部
		Ext.each(this.names, function(item, index, arr) {
					if (this.els[item]) {
						this.els[item].show();
					}
				}, this);
		// 窗口居中显示
		this.el.center(document.body);
		if ('on' != this.closeState) {
			this.els.close.hide();
		}
		// 调整遮盖和蒙板
		if (this.mask) {
			this.els.mask.setWidth(document.body.clientWidth);
			this.els.mask.setHeight(this.getHeight());
			this.els.mask.setOpacity(0.3);
			this.els.shim.setWidth(document.body.clientWidth);
			this.els.shim.setHeight(this.getHeight());
			this.els.shim.setOpacity(0);
		} else {
			this.els.mask.hide();
			this.els.shim.setWidth(this.els.el.getWidth());
			this.els.shim.setHeight(this.els.el.getHeight());
			this.els.shim.setOpacity(0);
			this.els.shim.setPositioning(this.els.el.getPositioning());
		}
		this.fireEvent("show", this);
	},
	enableClose : function() {
		this.closeState = 'on';
		this.els.close.show();
	},
	disableClose : function() {
		this.closeState = 'off';
		this.els.close.hide();
	},
	close : function() {
		Ext.each(this.names, function(item, index, arr) {
					if (this.els[item]) {
						this.els[item].hide();
					}
				}, this);
		this.fireEvent("close", this);
	},
	hide : function() {
		this.close();
		this.fireEvent("hide", this);
	},
	setTitle : function(title) {
		this.els.text.update(title ? title : "");
	},
	update : function(text) {
		this.els.content.update(text ? text : "");
	},
	getEl : function() {
		return this.win;
	}
});